package com.huahua.graduateschoolspringboot.controller;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.huahua.graduateschoolspringboot.entity.R;
import com.huahua.graduateschoolspringboot.entity.po.CityNatureGas;
import com.huahua.graduateschoolspringboot.entity.po.CityNatureGasDevice;
import com.huahua.graduateschoolspringboot.entity.vo.CityNatureGasCountVo;
import com.huahua.graduateschoolspringboot.entity.vo.NatureGasCountVo;
import com.huahua.graduateschoolspringboot.service.CityNatureGasDeviceService;
import com.huahua.graduateschoolspringboot.service.CityNatureGasService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author LoveHuaHua
 * @date 2023年05月08日 9:06
 * @description believe in yourself
 */
@RestController
@RequestMapping("/natureGas")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class NatureGasController {

    private final CityNatureGasService cityNatureGasService;
    private final CityNatureGasDeviceService cityNatureGasDeviceService;

    @GetMapping("/getList")
    public Object getList() {
        List<CityNatureGasDevice> list = cityNatureGasDeviceService.list();
        return R.success(CollStreamUtil.toIdentityMap(list,CityNatureGasDevice::getRoomName));
    }

    /**
     * 获取报警计数
     *
     * @return {@link Object}
     */
    @GetMapping("/getNatureGasCount")
    public Object getNatureGasCount() {
        CityNatureGasCountVo cityNatureGasCountVo = new CityNatureGasCountVo();
        Date date = new Date();
        Long natureGasCountDay = cityNatureGasService.lambdaQuery().between(CityNatureGas::getCreateTime, DateUtil.beginOfDay(date), DateUtil.endOfDay(date)).count();
        Long natureGasCountWeek = cityNatureGasService.lambdaQuery().between(CityNatureGas::getCreateTime, DateUtil.beginOfWeek(date), DateUtil.endOfWeek(date)).count();
        Long natureGasCountMonth = cityNatureGasService.lambdaQuery().between(CityNatureGas::getCreateTime, DateUtil.beginOfMonth(date), DateUtil.endOfMonth(date)).count();
        Long natureGasCountYear = cityNatureGasService.lambdaQuery().between(CityNatureGas::getCreateTime, DateUtil.beginOfYear(date), DateUtil.endOfYear(date)).count();
        cityNatureGasCountVo.setNatureGasCountDay(natureGasCountDay);
        cityNatureGasCountVo.setNatureGasCountWeek(natureGasCountWeek);
        cityNatureGasCountVo.setNatureGasCountMonth(natureGasCountMonth);
        cityNatureGasCountVo.setNatureGasCountYear(natureGasCountYear);
        return R.success(cityNatureGasCountVo);
    }

    /**
     * 今天得到报警计数
     *
     * @return {@link Object}
     */
    @GetMapping("/getTodayNatureGasCount")
    public Object getTodayNatureGasCount() {
        NatureGasCountVo natureGasCountVo = new NatureGasCountVo();
        ArrayList<Long> todayNatureGasCounts = new ArrayList<>();
        String today = DateUtil.today();
        for (int i = 0; i < 24; i++) {
            Long zeroClock = cityNatureGasService.lambdaQuery().between(CityNatureGas::getCreateTime, today + " " + StrUtil.fillBefore(String.valueOf(i), '0', 2) + ":00:00", today + " " + StrUtil.fillBefore(String.valueOf(i), '0', 2) + ":59:59").count();
            todayNatureGasCounts.add(zeroClock);
        }
        natureGasCountVo.setNatureGasCountHour(todayNatureGasCounts);

        return R.success(natureGasCountVo);
    }

    /**
     * 得到周报警计数
     *
     * @return {@link Object}
     */
    @GetMapping("/getWeekNatureGasCount")
    public Object getWeekNatureGasCount() {
        NatureGasCountVo natureGasCountVo = new NatureGasCountVo();
        //这周的开始时间
        ArrayList<Long> weekNatureGasCounts = new ArrayList<>();
        DateTime dateTime = DateUtil.beginOfWeek(new Date());
        for (int i = 0; i < 7; i++) {
            Long currentDay = cityNatureGasService.lambdaQuery().between(CityNatureGas::getCreateTime, DateUtil.beginOfDay(dateTime), DateUtil.endOfDay(dateTime)).count();
            weekNatureGasCounts.add(currentDay);
            //每次加一天
            dateTime = dateTime.offset(DateField.DAY_OF_MONTH, 1);
        }
        natureGasCountVo.setNatureGasCountWeek(weekNatureGasCounts);
        return R.success(natureGasCountVo);
    }

    /**
     * 得到月报警计数
     *
     * @return {@link Object}
     */
    @GetMapping("/getMonthNatureGasCount")
    public Object getMonthNatureGasCount() {
        NatureGasCountVo natureGasCountVo = new NatureGasCountVo();
        //月开始的时间
        DateTime ofYear = DateUtil.beginOfYear(new Date());
        ArrayList<Long> monthNatureGasCounts = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            Long currentMonth = cityNatureGasService.lambdaQuery().between(CityNatureGas::getCreateTime, DateUtil.beginOfMonth(ofYear), DateUtil.endOfMonth(ofYear)).count();
            monthNatureGasCounts.add(currentMonth);
            //每次加1个月
            ofYear.offset(DateField.MONTH,1);
        }
        natureGasCountVo.setNatureGasCountMonth(monthNatureGasCounts);
        return R.success(natureGasCountVo);
    }
}
